import { createRouter, createWebHashHistory } from "vue-router"
import Index from '../pages/Index.vue'
import User from '../pages/User.vue'
import Cart from '../pages/Cart.vue'
import Category from '../pages/Category.vue'
import Product from '../pages/Product.vue'
import UserLogin from '../pages/user/UserLogin.vue'
const router=createRouter({
    history:createWebHashHistory(),
    routes:[ //路由表
        // redirect重定向，当访问"/"时，路由表为我们重新指向“/index”组件
        {path:'/',redirect:"/index",meta:{title:'首页'}},
        {path:'/index',component:Index,meta:{title:'首页'}},
        {path:'/user',component:User,meta:{title:'个人中心'},
    children:[
        {path:'login',component:UserLogin},
        {path:'reg',component:()=>import ('../pages/user/UserReg.vue')},//懒加载，调用组件时才加载
    ]},
        {path:'/cart',component:Cart,meta:{title:'购物车'}},
        {path:'/cartproduct',name:"cartproduct",component:()=>import ('../pages/cart/CartProduct.vue'),meta:{title:'购物车详情'}},
        {path:'/category',component:Category,meta:{title:'商品分类'}},
        {path:'/product/:id/:type',name:'product',component:Product,meta:{title:'商品详情'},props:true},
        {path:'/:match(.*)*',component:()=>import ('../components/404.vue')},//处理找不到或者错误组件
    ],
    linkActiveClass: 'active' //路由响应时的样式
})
router.beforeEach((to,from,next)=>{
    document.title=to.meta.title;
    next()
})
export default router